using System.Threading.Tasks;
using Matrix.Configuration.Events;

namespace Matrix.Configuration.Services
{
    /// <summary>
    /// 事件持久化服务接口
    /// 负责事件的持久化存储和查询
    /// </summary>
    public interface IEventPersistenceService
    {
        /// <summary>
        /// 持久化配置变更事件
        /// </summary>
        /// <param name="eventData">配置变更事件数据</param>
        Task PersistEventAsync(ConfigurationChangedEto eventData);

        /// <summary>
        /// 持久化批量配置变更事件
        /// </summary>
        /// <param name="batchEventData">批量配置变更事件数据</param>
        Task PersistBatchEventAsync(ConfigurationBatchChangedEto batchEventData);

        /// <summary>
        /// 获取配置变更历史
        /// </summary>
        /// <param name="key">配置键</param>
        /// <param name="namespace">配置命名空间</param>
        /// <param name="maxCount">最大返回数量</param>
        /// <param name="tenantId">租户ID</param>
        Task<System.Collections.Generic.List<ConfigurationChangedEto>> GetChangeHistoryAsync(
            string key,
            string @namespace = "",
            int maxCount = 50,
            System.Guid? tenantId = null);

        /// <summary>
        /// 根据事件ID获取事件
        /// </summary>
        /// <param name="eventId">事件ID</param>
        Task<ConfigurationChangedEto?> GetEventByIdAsync(System.Guid eventId);

        /// <summary>
        /// 根据时间范围获取事件
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="tenantId">租户ID</param>
        Task<System.Collections.Generic.List<ConfigurationChangedEto>> GetEventsByTimeRangeAsync(
            System.DateTime startTime,
            System.DateTime endTime,
            System.Guid? tenantId = null);

        /// <summary>
        /// 删除过期的事件记录
        /// </summary>
        /// <param name="retentionDays">保留天数</param>
        Task CleanupExpiredEventsAsync(int retentionDays = 30);

        /// <summary>
        /// 获取事件统计信息
        /// </summary>
        /// <param name="tenantId">租户ID</param>
        Task<EventStatistics> GetEventStatisticsAsync(System.Guid? tenantId = null);
    }

    /// <summary>
    /// 事件统计信息
    /// </summary>
    public class EventStatistics
    {
        /// <summary>
        /// 总事件数
        /// </summary>
        public long TotalEvents { get; set; }

        /// <summary>
        /// 今日事件数
        /// </summary>
        public long TodayEvents { get; set; }

        /// <summary>
        /// 本周事件数
        /// </summary>
        public long WeekEvents { get; set; }

        /// <summary>
        /// 本月事件数
        /// </summary>
        public long MonthEvents { get; set; }

        /// <summary>
        /// 按重要性级别统计
        /// </summary>
        public System.Collections.Generic.Dictionary<ConfigurationImportanceLevel, long> EventsByImportance { get; set; } = new();

        /// <summary>
        /// 按变更来源统计
        /// </summary>
        public System.Collections.Generic.Dictionary<ConfigurationChangeSource, long> EventsBySource { get; set; } = new();

        /// <summary>
        /// 按配置分类统计
        /// </summary>
        public System.Collections.Generic.Dictionary<string, long> EventsByCategory { get; set; } = new();

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public System.DateTime LastUpdated { get; set; }
    }
}